﻿using System;
using System.Linq;
using System.Windows.Forms;
using QFlow.Core;
using QFlow.Data;

namespace QFlow.Windows.Forms
{
    public partial class SalesOrdersList : Utils.BaseForm
    {
        public SalesOrdersList(QFlowDbContext context)
            : base(context)
        {
            InitializeComponent();
            DevComponents.DotNetBar.Controls.GroupPanel.CheckForIllegalCrossThreadCalls = false;
        }

        private void SalesOrdersList_Load(object sender, EventArgs e)
        {
            cmbCustomer.DataSource = this.Context.Customers.ToArray();
            cmbMethod.DataSource = this.Context.PaymentMethods.ToArray();
            cmbStatus.DataSource = this.Context.OrderStatus.ToArray();

            cmbCustomer.SelectedIndex = -1;
            cmbMethod.SelectedIndex = -1;
            cmbStatus.SelectedIndex = -1;
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            RefereshGrid();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            NewSalesOrder frm = new NewSalesOrder(true, this.Context);
            frm.MdiParent = this.MdiParent;
            frm.Show();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            EditOrder();
        }

        private void EditOrder()
        {
            if (gvResults.SelectedRows.Count > 0)
            {
                SaleOrder order = gvResults.SelectedRows[0].DataBoundItem as SaleOrder;
                NewSalesOrder frm = new NewSalesOrder(order, this.Context);
                frm.MdiParent = this.MdiParent;
                frm.Show();
            }
        }

        private void gvResults_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (gvResults.Columns[e.ColumnIndex].Name != "btnReturn" && gvResults.Columns[e.ColumnIndex].Name != "btnCancel")
                EditOrder();
        }

        private void SalesOrdersList_Activated(object sender, EventArgs e)
        {
            RefereshGrid();
        }

        private void gvResults_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != -1)
            {
                if (gvResults.Columns[e.ColumnIndex].Name == "btnCancel")
                {
                    SaleOrder order = gvResults.Rows[e.RowIndex].DataBoundItem as SaleOrder;
                    if (order.StatusID == this.Context.OrderStatus.GetCanceledStatus().StatusID)
                    {
                        MessageBox.Show(QFlow.Windows.Properties.Resources.MBAlreayCanceled, QFlow.Windows.Properties.Resources.MBErrorCaption, MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                    }
                    else if (order.StatusID != this.Context.OrderStatus.GetAdjustedStatus().StatusID)
                    {
                        if (MessageBox.Show(QFlow.Windows.Properties.Resources.MBCancelOrder, QFlow.Windows.Properties.Resources.MBConfirmCaption, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                        {
                            SaleOrderManager.CancelSaleOrder(order);
                            RefereshGrid();
                        }
                    }
                }
                else if (gvResults.Columns[e.ColumnIndex].Name == "btnReturn")
                {
                    SaleOrder order = gvResults.Rows[e.RowIndex].DataBoundItem as SaleOrder;
                    if (order.StatusID != this.Context.OrderStatus.GetClosedStatus().StatusID)
                    {
                        MessageBox.Show(QFlow.Windows.Properties.Resources.MBReturnPaidOnly, QFlow.Windows.Properties.Resources.MBErrorCaption, MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                    }
                    else
                    {
                        ReturnSalesOrder frm = new ReturnSalesOrder(order, this.Context);
                        frm.MdiParent = this.MdiParent;
                        frm.Show();
                    }
                }
            }
        }

        private void RefereshGrid()
        {
            IQueryable<SaleOrder> lst = this.Context.SaleOrders;
            if (cmbCustomer.SelectedIndex != -1)
                lst = lst.Where(order => order.CustomerID == (int)cmbCustomer.SelectedValue);
            if (cmbMethod.SelectedIndex != -1)
                lst = lst.Where(order => order.PaymentMethodID == (int)cmbMethod.SelectedValue);
            if (cmbStatus.SelectedIndex != -1)
                lst = lst.Where(order => order.StatusID == (int)cmbStatus.SelectedValue);
            if (dtFrom.LockUpdateChecked)
                lst = lst.Where(order => order.Date.Date >= dtFrom.Value.Date);
            if (dtTo.LockUpdateChecked)
                lst = lst.Where(order => order.Date.Date <= dtTo.Value.Date);

            gvResults.AutoGenerateColumns = false;
            gvResults.DataSource = lst.ToArray();
        }

        private void gvResults_SelectionChanged(object sender, EventArgs e)
        {
            if (gvResults.SelectedRows.Count > 0)
                btnEdit.Enabled = true;
            else btnEdit.Enabled = false;
        }
    }
}
